const logStr=[];

export function compareForms(oldForm, newForm, formMap, path = []) {
    for (let key in oldForm) {
        if (!newForm.hasOwnProperty(key)) {
            // Key removed
            const mappedName = getMappedName(formMap, path.concat(key));
            // console.log(`${mappedName} 被移除`);
            logStr.push(`${mappedName} 被移除`)
        } else {
            compareValues(oldForm[key], newForm[key], formMap, path.concat(key));
        }
    }

    for (let key in newForm) {
        if (!oldForm.hasOwnProperty(key)) {
            // Key added
            const mappedName = getMappedName(formMap, path.concat(key));
            // console.log(`${mappedName} 新增`);
            logStr.push(`${mappedName} 新增`);
        }
    }

    return logStr;
}

function compareValues(oldVal, newVal, formMap, path) {
    const mappedName = getMappedName(formMap, path);
    
    if (typeof oldVal === 'object' && typeof newVal === 'object' && oldVal !== null && newVal !== null) {
        if (Array.isArray(oldVal) && Array.isArray(newVal)) {
            // Handle arrays
            compareArrays(oldVal, newVal, formMap, path);
        } else {
            // Handle objects
            compareForms(oldVal, newVal, formMap, path);
        }
    } else if (oldVal !== newVal) {
        // Simple value change
        // console.log(`${mappedName} 由 ${oldVal} 改为 ${newVal}`);
        logStr.push(`${mappedName} 由 ${oldVal} 改为 ${newVal}`);
    }
}

function compareArrays(oldArr, newArr, formMap, path) {
    const mappedName = getMappedName(formMap, path);

    // Check for item removals and additions
    const oldItems = oldArr.map(e => typeof e === 'object' ? e[Object.keys(formMap[path[path.length - 1]])[0]] : e);
    const newItems = newArr.map(e => typeof e === 'object' ? e[Object.keys(formMap[path[path.length - 1]])[0]] : e);

    const removedItems = oldItems.filter(x => !newItems.includes(x));
    const addedItems = newItems.filter(x => !oldItems.includes(x));

    if (removedItems.length > 0) {
        // console.log(`${mappedName} 减少了: ${removedItems.join(', ')}`);
        logStr.push(`${mappedName} 减少了: ${removedItems.join(', ')}`);
    }
    if (addedItems.length > 0) {
        // console.log(`${mappedName} 新增了: ${addedItems.join(', ')}`);
        logStr.push(`${mappedName} 新增了: ${addedItems.join(', ')}`);
    }

    // Check for position changes in array of objects
    if (typeof oldArr[0] === 'object') {
        oldArr.forEach((item, index) => {
            const key = Object.keys(formMap[path[path.length - 1]])[0]; // Get the key to compare, e.g., 'name'
            const newItemIndex = newArr.findIndex(i => i[key] === item[key]);
            if (newItemIndex !== -1 && newItemIndex !== index) {
                // console.log(`在${mappedName}中, ${formMap[path[path.length - 1]][key]} ${item[key]} 位置由 ${index} 改变为 ${newItemIndex}`);
                logStr.push(`在${mappedName}中, ${formMap[path[path.length - 1]][key]} ${item[key]} 位置由 ${index} 改变为 ${newItemIndex}`);
            }
        });
    }
}

function getMappedName(formMap, path) {
    let currentMap = formMap;
    for (const part of path) {
        if (currentMap.hasOwnProperty(part)) {
            currentMap = currentMap[part];
            if (typeof currentMap === 'string') return currentMap; 
        } else {
            break;
        }
    }
    return path[path.length - 1]; 
}

